﻿# -*- coding: cp936 -*-
from django.http import HttpResponse
from django.template import loader, Context
import urllib2
import re
import os
import csv
from wp.t2.models import *

def catch_handler_html(request):
	o = '<title>test_catch_html</title>'
	o += '<a>hello world</a>'
	l = 'http://www.pfwx.com/files/article/html/10/10616/1889786.html'
	req = urllib2.Request(l)
	response = urllib2.urlopen(req)
	page = response.read()
	
	#parser = Test_Parser()
	#parser.feed(page)
	#o += parser.getcontent()
	n = page.find('<div id="content">')
	m = page.find('<DIV align="center">')
	page = page[n:m].replace('<script language="javascript" src="/acc/style12.js"></script>', '')
	return HttpResponse(page)

def jxys_index(request):
	index = 'http://www.pfwx.com/files/article/html/10/10616/'
	page_index = urllib2.urlopen(urllib2.Request(index + 'index.html')).read()
	r = re.compile(r'\"\d{7}.html\">\W+</a>').findall(page_index)
	n = 0
	o = """<html><head><title>Test Catch JXYS</title>
	<link rel="stylesheet" type="text/css" href="/media/test.css"/></head>
	<body><table><tr>
	"""
	for l in r:
		n += 1
		o += '<td><a href = ' + l + '</td>\n'
		if n % 4 == 0:
			o += '</tr><tr>'
	o += '</tr></table></body></html>'
	return HttpResponse(o.decode('GBK'))
	
def jxys_each_handler(request, n):
    l = 'http://www.pfwx.com/files/article/html/10/10616/' + n
    page = urllib2.urlopen(urllib2.Request(l)).read()
    n = page.find('<div id="content">')
    m = page.find('<DIV align="center">')

    page = page[n:m].replace('<script language="javascript" src="/acc/style12.js"></script>', 
    '<link rel="stylesheet" type="text/css" href="/media/css/base.css"/>')
    
    
    return HttpResponse(page.decode('GBK'))
	
def jxys_all(request):
	index = 'http://www.pfwx.com/files/article/html/10/10616/'
	page_index = urllib2.urlopen(urllib2.Request(index + 'index.html')).read()
	r = re.compile(r"\d{7}.html").findall(page_index)
	o = '<title>Test Catch JXYS</title>'
	for l in r:
		l = 'http://www.pfwx.com/files/article/html/10/10616/' + l
		page = urllib2.urlopen(urllib2.Request(l)).read()
		n = page.find('<div id="content">')
		m = page.find('<DIV align="center">')
		page = page[n:m].replace('<script language="javascript" src="/acc/style12.js"></script>', 
		'<link rel="stylesheet" type="text/css" href="/media/css/base.css"/>')
		tch = test_catch_html(href=l, title='', content=page.decode('GBK'))
		tch.save()
		print l
		n = page.find('<div id="content">')
		m = page.find('<DIV align="center">')
		o += page
	return HttpResponse(o)
	
def test_insert_sqlite(request):
	test_content = 'a'
	i = 0
	while i < 100000000:
		i += 1
		t = test_img(img = test_content)
		t.save()
		if(i %1000 == 1):
			print i
	return HttpResponse('finish')

def test_insert2():
	test_content = 'a'
	i = 0
	while i < 100000000:
		i += 1
		t = test_img(img = test_content)
		t.save()
		if(i %10000 == 1):
			print i

def test_select(request):
	q = TestInsert.objects.all()
	t = loader.get_template("test/test_select.html")
	c = Context({'es': q })
	return HttpResponse(t.render(c))
	
def test_decode(response):
	return HttpResponse('你好,测试成功!'.decode('GBK'))

def test_decode_template(response):
	t = loader.get_template("test/test_decode.html")
	c = Context({'test':'测试字符串'.decode('GBK')})
	return HttpResponse(t.render(c))

def test_store_img(response):
	if request.method == 'POST':
		img = test_img()
		img = request.POST['img']
	else:
		return HttpResponse("""TEST CASE
						<form method = "post" action = "">
						<input type="file" name = "img"/>
						<input type = "submit"/></form>
						""")
def test_blob(response):
	pass 
		
def test_gcc_process(response):
	os.chdir('/kc/static/kc/tmp')
	os.system('mkdir 5073709057')
	os.chdir('5073709057')
	f = open('hello.cpp', 'w')
	f.write("""
	#include<iostream>
	using namespace std;
	int main()
	{
		cout<<"hello world"; 
		return 0;
	}
	""")
	f.close()
	os.system('g++ hello.cpp -o hello')
	os.system('hello > output')
	f = open('output', 'r')
	r = f.read()
	f.close()
	#os.system('del /S/Q *')
	#os.chdir('/kc/static/kc/tmp')
	os.chdir('..')
	os.system('rmdir /Q 5073709057')
	return HttpResponse(r)
	
def test_csv(request):
	#	return event_register(request, 5)
	intel_id = 5
	_e = event.objects.get(id = intel_id) # 5 is just the id of Intel visit

	if request.method == 'POST':
		name = request.POST['name']
		sid = request.POST['sid']
		classno = request.POST['classno']
		phone = request.POST['phone']
		email = request.POST['email']
		_event = event.objects.get(id = intel_id)
		
		cw = csv.writer(open('E:/kc/static/kc/tmp/test.csv', 'a'), delimiter=',', quotechar = '|', quoting=csv.QUOTE_MINIMAL)
		cw.writerow([name, sid, classno, phone, email])
		msg = '<h1>Thanks!</h1>'
		t = loader.get_template("reg_v2_submit.html")
		return HttpResponse(t.render(Context({'msg': msg})))
		
	else:
		t = loader.get_template("reg_v2.html")
		c = Context({'title':_e.name ,'e': _e})
		return HttpResponse(t.render(c))
		
